package com.carrental.finance.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.finance.dto.RefundDTO;
import com.carrental.finance.entity.Refund;
import com.carrental.finance.service.RefundService;
import com.carrental.finance.vo.RefundVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 退款控制器
 */
@Api(tags = "退款管理")
@RestController
@RequestMapping("/refunds")
public class RefundController {

    @Autowired
    private RefundService refundService;

    @ApiOperation("创建退款记录")
    @PostMapping
    public Result<Long> createRefund(@RequestBody @Validated RefundDTO refundDTO) {
        Long id = refundService.createRefund(refundDTO);
        return Result.success(id);
    }

    @ApiOperation("根据ID获取退款详情")
    @GetMapping("/{id}")
    public Result<RefundVO> getRefundById(@PathVariable Long id) {
        RefundVO refundVO = refundService.getRefundById(id);
        return Result.success(refundVO);
    }

    @ApiOperation("根据退款单号获取退款详情")
    @GetMapping("/no/{refundNo}")
    public Result<RefundVO> getRefundByRefundNo(@PathVariable String refundNo) {
        RefundVO refundVO = refundService.getRefundByRefundNo(refundNo);
        return Result.success(refundVO);
    }

    @ApiOperation("根据支付ID获取退款详情")
    @GetMapping("/payment/{paymentId}")
    public Result<RefundVO> getRefundByPaymentId(@PathVariable Long paymentId) {
        RefundVO refundVO = refundService.getRefundByPaymentId(paymentId);
        return Result.success(refundVO);
    }

    @ApiOperation("根据订单ID分页查询退款记录")
    @GetMapping("/order/{orderId}")
    public Result<Page<RefundVO>> pageOrderRefunds(
            @PathVariable Long orderId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
        Page<Refund> page = new Page<>(current, size);
        Page<RefundVO> result = refundService.pageOrderRefunds(orderId, page);
        return Result.success(result);
    }

    @ApiOperation("根据用户ID分页查询退款记录")
    @GetMapping("/user/{userId}")
    public Result<Page<RefundVO>> pageUserRefunds(
            @PathVariable Long userId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size) {
        Page<Refund> page = new Page<>(current, size);
        Page<RefundVO> result = refundService.pageUserRefunds(userId, page);
        return Result.success(result);
    }

    @ApiOperation("分页查询退款记录")
    @GetMapping("/page")
    public Result<Page<RefundVO>> pageRefunds(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("退款单号") @RequestParam(required = false) String refundNo,
            @ApiParam("支付单号") @RequestParam(required = false) String paymentNo,
            @ApiParam("订单号") @RequestParam(required = false) String orderNo,
            @ApiParam("用户ID") @RequestParam(required = false) Long userId,
            @ApiParam("退款类型") @RequestParam(required = false) Integer refundType,
            @ApiParam("退款状态") @RequestParam(required = false) Integer status,
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        Page<Refund> page = new Page<>(current, size);
        Page<RefundVO> result = refundService.pageRefunds(page, refundNo, paymentNo, orderNo, userId, refundType, status, startTime, endTime);
        return Result.success(result);
    }

    @ApiOperation("处理退款回调")
    @PostMapping("/callback")
    public Result<Boolean> handleRefundCallback(
            @ApiParam("退款单号") @RequestParam String refundNo,
            @ApiParam("交易流水号") @RequestParam String transactionId,
            @ApiParam("退款状态") @RequestParam Integer status) {
        boolean success = refundService.handleRefundCallback(refundNo, transactionId, status);
        return Result.success(success);
    }

    @ApiOperation("取消退款")
    @PostMapping("/cancel/{id}")
    public Result<Boolean> cancelRefund(@PathVariable Long id) {
        boolean success = refundService.cancelRefund(id);
        return Result.success(success);
    }
} 